﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TouristAgencyDataLayer
{
    public class Persons
    {
        public static int CreatePerson(string  fName, string lName, string passNum)
        {
            if (!string.IsNullOrEmpty(fName) && !string.IsNullOrEmpty(lName) && !string.IsNullOrEmpty(passNum))
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                Person m = new Person()
                {
                    FirstName = fName,
                    LastName = lName,
                    PassportNumber = passNum
                };

                context.Persons.InsertOnSubmit(m);
                context.SubmitChanges();

                return m.PersonId;
            }

            return -1;
        }

        public static void UpdatePerson(int perId, string fName, string lName, string passNum)
        {
            if (!string.IsNullOrEmpty(fName) && !string.IsNullOrEmpty(lName) && !string.IsNullOrEmpty(passNum))
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                var res =
                    (from b in context.Persons
                     where b.PersonId == perId
                     select b).FirstOrDefault();

                if (res != null)
                {
                    res.FirstName = fName;
                    res.LastName = lName;
                    res.PassportNumber = passNum;
                    context.SubmitChanges();
                }
            }
        }

        public static void DeletePerson(int perId)
        {

            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var per =
                (from b in context.Persons
                 where b.PersonId == perId
                 select b).FirstOrDefault();

            if (per != null)
            {
                context.Persons.DeleteOnSubmit(per);
                context.SubmitChanges();
            }
        }
    }
}
